package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallComment;
import com.cskaoyan.bean.CskaoyanmallUser;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.CommentService;
import com.cskaoyan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wx/comment")
public class WxCommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    UserService userService;

    @GetMapping("/count")
    public Object count(Integer valueId,Integer type){
        Integer hasPicCount = commentService.selectHasPicCount(valueId);
        Integer allCount = commentService.selectAllCount(valueId);
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo(hasPicCount, allCount);
        return BaseRespVo.success(wxCommentCountVo);
    }

    @RequestMapping("/list")
    public Object list(Integer valueId){
        List<Integer> userIdList = commentService.selectUserIdByValueId(valueId);
        List<CskaoyanmallComment> commentList = commentService.selecAllByValueId(valueId);
        Integer count = commentService.selectAllCount(valueId);
        List<WxCommenDataVo> list = new ArrayList<>();
        for (int i = 0; i < userIdList.size(); i++) {
            Integer userId = userIdList.get(i);
            CskaoyanmallUser user = userService.selectAllById(userId);
            CskaoyanmallComment comment = commentList.get(i);
            WxCommentUseInfoVo wxCommentUseInfoVo = new WxCommentUseInfoVo(user.getNickname(),user.getAvatar());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String addTime = simpleDateFormat.format(comment.getAddTime());
            WxCommenDataVo wxCommenDataVo = new WxCommenDataVo(wxCommentUseInfoVo,addTime,comment.getPicUrls(),comment.getContent());
            list.add(wxCommenDataVo);
        }
        WxCommentListVo wxCommentListVo = new WxCommentListVo(list,count,1);
        return BaseRespVo.success(wxCommentListVo);
    }
}
